#!/bin/sh

source scripts/common.sh

####################################################################################################

GLEW_VERSION=1.5.2
GLEW_BASENAME="glew"
GLEW_ARCHIVE="${GLEW_BASENAME}-${GLEW_VERSION}.tgz"
GLEW_DIR="${GLEW_BASENAME}-${GLEW_VERSION}"
GLEW_URL="http://prdownloads.sourceforge.net/${GLEW_BASENAME}/${GLEW_VERSION}/${GLEW_ARCHIVE}"
GLEW_PREFIX="${THIRD_PARTY_PREFIX}/${GLEW_BASENAME}"
GLEW_LIB=libGLEW.a

####################################################################################################

function build_glew()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD} || exit 1
pushd ${THIRD_PARTY_BUILD} || exit 1

report ""
report "Removing old build from '${GLEW_DIR}'..."
rm -fr $GLEW_DIR
rm -fr $GLEW_PREFIX/${target}

report "Preparing ${target} '${GLEW_BASENAME}'..."
rm -fr $GLEW_ARCHIVE
fetch $GLEW_URL $GLEW_ARCHIVE

report "Configuring ${target} '${GLEW_BASENAME}'..."
report ""

pushd ${GLEW_DIR} || exit 1
mkdir -p ${target} || exit 1

report "Building ${target} '${GLEW_BASENAME}'..."

opt="-O3"
if [ "$target" == "i686" ] 
then
	opt="-O3 -msse -msse2 -msse3 -msse4.2 -msse4a"
fi

if [ "$target" == "x86_64" ] 
then
	opt="-O3 -msse -msse2 -msse3 -msse4.2 -msse4a"
fi

nice make -j4 lib/${GLEW_LIB} \
		CXXFLAGS="${SYSROOT} -arch ${target} -I${THIRD_PARTY_BUILD}/${GLEW_DIR}/include ${opt} " \
		CFLAGS="${SYSROOT} -arch ${target} -I${THIRD_PARTY_BUILD}/${GLEW_DIR}/include ${opt} " \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK}" \
		GLEW_DEST="${GLEW_PREFIX}/${target}" || exit 1

nice make install \
		GLEW_DEST="${GLEW_PREFIX}/${target}" || exit 1

report "Built ${target} '${GLEW_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	build_glew $target
done

create_universal_lib ${GLEW_PREFIX} ${GLEW_LIB}
create_intel_lib ${GLEW_PREFIX} ${GLEW_LIB}
create_powerpc_lib ${GLEW_PREFIX} ${GLEW_LIB}

####################################################################################################

